/**
 *
 */
package com.angel.io.type.rows.impl;

import java.text.DateFormat;
import java.util.Date;

import com.angel.common.helpers.StringHelper;
import com.angel.io.exceptions.InvalidRowValueAccessException;
import com.angel.io.separator.ColumnSeparator;
import com.angel.io.type.rows.ExportRowFile;

/**
 * @author William
 *
 */
public class OutlookRowFile implements ExportRowFile{

	private ColumnSeparator separator;
	private String[] cols;
	private HeaderRowFile headerRowFile;

	public OutlookRowFile(String row, ColumnSeparator columnSeparator, HeaderRowFile headerRowFile){
		super();
		this.separator = columnSeparator;
		this.cols = row.split(separator.getValue());
		this.headerRowFile = headerRowFile;
		this.initializeColumns();
	}


	public OutlookRowFile(ColumnSeparator columnSeparator, Integer quantityColumns, HeaderRowFile header) {
		super();
		this.separator = columnSeparator;
		this.cols = new String[quantityColumns];
		this.headerRowFile = header;
		this.initializeColumns();
	}


	private void initializeColumns() {
		for(int i = 0; i < this.headerRowFile.quantityColumns(); i++){
			this.cols[i] = this.headerRowFile.getColumnName(i);
		}
	}


	public void addDateValue(Date value, DateFormat dateFormat, int position) {
		this.addValue(dateFormat.format(value), position);
	}


	public void addFloatValue(Float value, int position) {
		this.addValue(value, position);
	}


	public void addIntegerValue(Integer value, int position) {
		this.addValue(value.toString(), position);
	}


	public void addStringValue(String value, int position) {
		this.addValue(value, position);
	}


	public void addValue(Object value, int position) {
		this.checkPosition(position);
		String columnName = this.getHeaderRowFile().getColumnName(position);
		this.cols[position] = columnName.concat(value.toString());
	}


	public int quantityColumns() {
		return this.cols != null ? this.cols.length: 0;
	}

	public void checkPosition(int position){
		if((this.quantityColumns() < position) || (this.cols == null)){
			throw new InvalidRowValueAccessException("Invalid access to position [" + position + "]. Row contains [" + ((this.cols != null) ? this.cols.length: 0)+ "] positions.");
		}
	}


	public String export() {
		String export = "";
		for(String col: this.cols){
			export += col + this.getSeparator().getValue();
		}
		return export;
	}

	public void addDoubleValue(Double value, int position) {
		this.checkPosition(position);
		this.cols[position] = value.toString();
	}

	public boolean isEmpty() {
		for(String value : this.cols){
			if(!StringHelper.isEmpty(value)){
				return false;
			}
		}
		return true;
	}


	/**
	 * @return the separator
	 */
	protected ColumnSeparator getSeparator() {
		return separator;
	}


	/**
	 * @param separator the separator to set
	 */
	protected void setSeparator(ColumnSeparator separator) {
		this.separator = separator;
	}


	/**
	 * @return the headerRowFile
	 */
	protected HeaderRowFile getHeaderRowFile() {
		return headerRowFile;
	}


	/**
	 * @param headerRowFile the headerRowFile to set
	 */
	protected void setHeaderRowFile(HeaderRowFile headerRowFile) {
		this.headerRowFile = headerRowFile;
	}


}
